package org.road0329.tools.proto_parse_tool.utils;

import java.lang.management.ManagementFactory;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Pattern;

public class DateUtil {
    /* 标准格式 */
    public static String TIME_LONG = "yyyy-MM-dd HH:mm:ss";
    public static String TIME_TOO_LONG = "yyyy-MM-dd HH:mm:ss.SSS";
    public static String TIME_NO_SEC = "yyyy-MM-dd HH:mm";
    public static String TIME_HOUR = "yyyy-MM-dd HH";
    //    public static String DATE_AND_MONTH = "yyyy-MM-dd HH";
    public static String MONTH_DATE_NO_SEC = "MM-dd HH:mm";
    public static String TIME_SHORT = "yyyy-MM-dd";
    public static String MONTHTIME = "yyyy-MM";
    public static String HOUR_MIN_SEC = "HH:mm:ss";
    public static String HOUR_MIN = "HH:mm";

    /* 中文格式 */
    public static String TIME_LONG_CN = "yyyy年MM月dd日 HH:mm:ss";
    public static String TIME_NO_MIN_CN = "yyyy年MM月dd日HH时";
    public static String TIME_SHORT_CN = "yyyy年MM月dd日";
    public static String MONTHTIME_CN = "yyyy年MM月";
    public static String TIME_SHORT_CN_S = "yy年MM月dd日";
    public static String DAY_HOUR_MIN_CN = "dd日HH:mm";
    public static String YEAR_LONG_CN = "yyyy年";

    /*简洁格式*/
    public static String TIME_LONG_17 = "yyyyMMddHHmmssSSS";
    public static String TIME_LONG_14 = "yyyyMMddHHmmss";
    public static String TIME_LONG_12 = "yyMMddHHmmss";
    public static String TIME_SHORT_8 = "yyyyMMdd";
    public static String TIME_SHORT_6 = "yyMMdd";
    public static String YEARMONTH = "yyyyMM";
    public static String MONTH_DAY_HOUR = "MMddHH";
    public static String MONTH_DAY_HOUR_MIN = "MMddHHmm";


    public static String TIME_NO_SEC_10 = "yyMMddHHmm";
    public static String YEAR_MONTH = "yyMM";


    public static String YEAR_LONG = "yyyy";
    public static String YEAR = "yy";
    public static String HOUR = "HH";
    public static String DAY = "dd";


    public static int PRE_ONE_MONTH = -1; // 上1个月
    public static int PRE_THREE_MONTH = -3; // 上3个月

    public static long FIRSTYEAR_2000 = 0;

    static {

        SimpleDateFormat sf = new SimpleDateFormat(TIME_LONG_17);
        try {
            //2000-1-1 00:00:00:000
            FIRSTYEAR_2000 = sf.parse("20000101000000000").getTime();
        } catch (ParseException e) {
            ////log.warn("simple date format parse error.", e);
        }
    }

    /**
     * 得到每月的最大日期数字
     *
     * @param date
     * @return
     */
    public static int getMaxMonthDay(Date date) {
        Calendar cr = Calendar.getInstance();
        cr.setTime(date);
        cr.set(Calendar.DAY_OF_MONTH, 1);
        cr.roll(Calendar.DAY_OF_MONTH, -1);
        return cr.get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 得到两个月中的最大日期数字
     */
    public static int getMaxMonthDay(Date date1, Date date2) {
        int maxDay1 = getMaxMonthDay(date1);
        int maxDay2 = getMaxMonthDay(date2);
        int maxDay;
        if (maxDay1 > maxDay2) {
            maxDay = maxDay1;
        } else {
            maxDay = maxDay2;
        }
        return maxDay;
    }

    /**
     * 日期按照HH:mm格式转化字符串
     *
     * @param date
     * @return
     */
    public static String DateToHHMMStr(Date date) {
        return DateToStr(date, HOUR_MIN);
    }

    /**
     * 日期按照HH:mm:ss格式转化字符串
     *
     * @param date
     * @return
     */
    public static String DateToHHMMSSStr(Date date) {
        return DateToStr(date, HOUR_MIN_SEC);
    }

    /**
     * 日期按照HH格式转化字符串
     *
     * @param date
     * @return
     */
    public static String DateToHHStr(Date date) {
        return DateToStr(date, HOUR);
    }

    /**
     * 日期按照dd格式转化字符串
     *
     * @param date
     * @return
     */
    public static String DateToDayStr(Date date) {
        return DateToStr(date, DAY);
    }

    /**
     * 日期按照yy格式转换字符串
     *
     * @param date
     * @return
     */
    public static String DateToYYStr(Date date) {
        return DateToStr(date, YEAR);
    }

    /**
     * 日期按照yyyy格式转换字符串
     *
     * @param date
     * @return
     */
    public static String DateToYYYYStr(Date date) {
        return DateToStr(date, YEAR_LONG);
    }

    /**
     * 日期按照yyMM格式转换字符串
     *
     * @param date
     * @return
     */
    public static String DateToYYMMStr(Date date) {
        return DateToStr(date, YEAR_MONTH);
    }

    public static String DateToYYYYMMStr(Date date) {
        return DateToStr(date, MONTHTIME);
    }

    public static String DateToYYYYMMStr2(Date date) {
        return DateToStr(date, MONTHTIME_CN);
    }

    /**
     * 日期按照yyyy-MM-dd HH:mm:ss格式格式转换字符串
     *
     * @param date
     * @return
     */
    public static String DateToLongStr(Date date) {
        return DateToStr(date, TIME_LONG);
    }

    /**
     * 日期按照yyyyMMddHHmmss格式格式转换字符串
     *
     * @param date
     * @return
     */
    public static String DateToLong14Str(Date date) {
        return DateToStr(date, TIME_LONG_14);
    }

    /**
     * 日期按照yyyy-MM-dd格式格式转换字符串
     *
     * @param date
     * @return
     */
    public static String DateToShortStr(Date date) {
        return DateToStr(date, TIME_SHORT);
    }

    /**
     * 按照指定格式格式转换字符串
     *
     * @param date
     * @param Format
     * @return
     */
    public static String DateToStr(Date date, String Format) {
        if (date == null)
            return "";
        SimpleDateFormat formater = new SimpleDateFormat(Format);
        return formater.format(date);
    }

    /**
     * 日期按照yyyy-MM-dd HH:mm格式转化字符串
     *
     * @param date
     * @return
     */
    public static String DateToNosecStr(Date date) {
        return DateToStr(date, TIME_NO_SEC);
    }

    public static String DateToNosec10Str(Date date) {
        return DateToStr(date, TIME_NO_SEC_10);
    }

    public static String DateToMMddHHStr(Date date) {
        return DateToStr(date, MONTH_DAY_HOUR);
    }

    /**
     * 日期按照yyyy年MM月dd日HH时格式转化字符串
     *
     * @param date
     * @return
     */
    public static String DateToNominStrCN(Date date) {
        return DateToStr(date, TIME_NO_MIN_CN);
    }

    /**
     * 微秒数转日期字符串
     */
    public static String UsecToDateStr(Long value) {
        if (value == null) {
            return null;
        }
        long microsecond = value % 1000;
        Date date = new Date(value / 1000);
        return DateUtil.DateToStr(date, DateUtil.TIME_TOO_LONG) + microsecond;
    }

    /**
     * 指定的格式yyyy-MM-dd HH:mm:ss的字符串按照转换为日期
     *
     * @param dateStr
     * @return
     */
    public static Date LongStrToDate(String dateStr) {
        if (dateStr == null) {
            return null;
        }
        if (dateStr.length() > 16) {
            return StrToDate(dateStr, TIME_LONG);
        } else if (dateStr.length() > 13) {
            return StrToDate(dateStr, TIME_NO_SEC);
        } else if (dateStr.length() > 9) {
            return StrToDate(dateStr, TIME_SHORT);
        } else if (dateStr.length() > 6) {
            return StrToDate(dateStr, MONTHTIME);
        } else if (dateStr.length() > 3) {
            return StrToDate(dateStr, "yyyy");
        } else {
            return null;
        }
    }

    /**
     * 指定的格式yyyy-MM-dd的字符串按照转换为日期
     *
     * @param dateStr
     * @return
     */
    public static Date ShortStrToDate(String dateStr) {
        return StrToDate(dateStr, TIME_SHORT);
    }

    /**
     * 指定的格式yyyy-MM-dd HH:mm的字符串按照转换为日期
     *
     * @param dateStr
     * @return
     */
    public static Date NosecStrToDate(String dateStr) {
        return StrToDate(dateStr, TIME_NO_SEC);
    }

    /**
     * 指定的格式的字符串按照转换为日期
     *
     * @param dateStr
     * @param Format
     * @return
     */
    public static Date StrToDate(String dateStr, String Format) {
        SimpleDateFormat formater = new SimpleDateFormat(Format);
        try {
            return formater.parse(dateStr);
        } catch (ParseException e) {
            //log.error("时间字符串转日期异常", e);
            return null;
        }
    }

    /**
     * 得到当前时间的开始的第一年
     *
     * @param date
     * @return
     */
    public static Date getFirstYear(Date date) {
        if (date != null) {
            Calendar cr = Calendar.getInstance();
            cr.setTime(date);
            cr.set(Calendar.MONDAY, 0);
            cr.set(Calendar.DAY_OF_MONTH, 1);
            cr.set(Calendar.HOUR_OF_DAY, 0);
            cr.set(Calendar.MINUTE, 0);
            cr.set(Calendar.SECOND, 0);
            cr.set(Calendar.MILLISECOND, 0);
            return cr.getTime();
        }

        return date;
    }

    /**
     * 获取后count年的date
     *
     * @param date
     * @param count
     * @return
     */
    public static Date getADDYear(Date date, int count) {
        Calendar cr = Calendar.getInstance();
        cr.setTime(date);
        cr.add(Calendar.YEAR, count);
        return cr.getTime();
    }

    /**
     * 计算两个任意时间中间的间隔天数
     *
     * @param startday
     * @param endday
     * @return
     */
    public static int getIntervalDays(Date startday, Date endday) {
        if (startday.after(endday)) {
            Date cal = startday;
            startday = endday;
            endday = cal;
        }
        long sl = startday.getTime();
        long el = endday.getTime();
        long ei = el - sl;
        return (int) (ei / (1000 * 60 * 60 * 24));
    }

    public static int getIntervalHours(Date startday, Date endday) {
        if (startday.after(endday)) {
            Date cal = startday;
            startday = endday;
            endday = cal;
        }
        long sl = startday.getTime();
        long el = endday.getTime();
        long ei = el - sl;
        return (int) (ei / (1000 * 60 * 60));
    }

    public static int getIntervalMinutes(Date startday, Date endday) {
        if (startday.after(endday)) {
            Date cal = startday;
            startday = endday;
            endday = cal;
        }
        long sl = startday.getTime();
        long el = endday.getTime();
        long ei = el - sl;
        return (int) (ei / (1000 * 60));
    }

    public static int getIntervalSecond(Date startday, Date endday) {
        if (startday.after(endday)) {
            Date cal = startday;
            startday = endday;
            endday = cal;
        }
        long sl = startday.getTime();
        long el = endday.getTime();
        long ei = el - sl;
        return (int) (ei / 1000);
    }

    /**
     * 得到分钟数字
     *
     * @param date
     * @return
     */
    public static int getSecondValue(Date date) {
        Calendar cr = Calendar.getInstance();
        cr.setTime(date);
        return cr.get(Calendar.SECOND);
    }

    /**
     * 得到分钟数字
     *
     * @param date
     * @return
     */
    public static int getMinuteValue(Date date) {
        Calendar cr = Calendar.getInstance();
        cr.setTime(date);
        return cr.get(Calendar.MINUTE);
    }

    /**
     * 得到date的 小时数字
     *
     * @param date
     * @return
     */
    public static int getHourValue(Date date) {
        Calendar cr = Calendar.getInstance();
        cr.setTime(date);
        return cr.get(Calendar.HOUR_OF_DAY);
    }

    /**
     * 得到date的日数字
     *
     * @param date
     * @return
     */
    public static int getDayValue(Date date) {
        Calendar cr = Calendar.getInstance();
        cr.setTime(date);
        return cr.get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 1～7:星期一~星期日
     *
     * @param date
     * @return
     */
    public static int getWeekValue(Date date) {
        Calendar cr = Calendar.getInstance();
        cr.setTime(date);
        int week = cr.get(Calendar.DAY_OF_WEEK);
        return week - 1;
    }

    /**
     * 得到date的月数字
     *
     * @param date
     * @return
     */
    public static int getMonthValue(Date date) {
        Calendar cr = Calendar.getInstance();
        cr.setTime(date);
        return cr.get(Calendar.MONTH) + 1;
    }

    /**
     * 得到date的年数字
     *
     * @param date
     * @return
     */
    public static int getYearValue(Date date) {
        Calendar cr = Calendar.getInstance();
        cr.setTime(date);
        return cr.get(Calendar.YEAR);
    }

    /**
     * 得到每个月的最后一天
     *
     * @param date 传入的需要比较的时间
     * @return
     */
    public static Date getMonthLastDay(Date date) {
        Calendar a = Calendar.getInstance();
        a.setTime(date);
        a.set(Calendar.DATE, 1); // 把日期设置为当月第一天
        a.roll(Calendar.DATE, -1);// 日期回滚一天，也就是最后一天
        a.set(Calendar.HOUR, 0);
        a.set(Calendar.MINUTE, 0);
        a.set(Calendar.SECOND, 0);
        a.set(Calendar.MILLISECOND, 0);
        return a.getTime();
    }

    public static int getMondayPlus(Date date) {
        Calendar cd = Calendar.getInstance();
        cd.setTime(date);
        // 获得今天是一周的第几天，星期日是第一天，星期二是第二天......
        int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK);
        if (dayOfWeek == 1) {
            return -6;
        } else {
            return 2 - dayOfWeek;
        }
    }

    public static Boolean isWorkDay(Date date) {
        Calendar cd = Calendar.getInstance();
        cd.setTime(date);
        // 获得今天是一周的第几天，星期日是第一天，星期二是第二天......
        int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK);
        if (dayOfWeek == 1 || dayOfWeek == 7) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 得到当前季度季度
     *
     * @param date
     * @return 1, 2, 3, 4
     */
    public static int getQuarterNumber(Date date) {
        int month = DateUtil.getMonthValue(date);
        if (month < 4) {
            return 1;
        } else if (month < 7) {
            return 2;
        } else if (month < 10) {
            return 3;
        } else {
            return 4;
        }
    }

    public static Date getLastDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int month = calendar.get(Calendar.MONTH);
        calendar.set(Calendar.MONTH, month + 1);
        calendar.set(Calendar.DATE, 0);
        return calendar.getTime();
    }

    public static Date getLastMonthDayHourOfYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.MONTH, 11);
        int month = calendar.get(Calendar.MONTH);
        calendar.set(Calendar.MONTH, month + 1);
        calendar.set(Calendar.DATE, 0);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    public static Date getFirstDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DATE, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    // 月25日
    public static Date get25Day(Date date, int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        // calendar.set(Calendar.DATE, 0);
        calendar.set(Calendar.DAY_OF_MONTH, day);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    public static Date getFirstHour(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        // calendar.set(Calendar.DATE, 0);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    public static Date getPreHour(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        // calendar.set(Calendar.DATE, 0);
        calendar.set(Calendar.HOUR_OF_DAY, getHourValue(date) - 1);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    public static Date getLastHour(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        // calendar.set(Calendar.DATE, 0);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    public static Date getSomeHour(Date date, int hour) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        // calendar.set(Calendar.DATE, 0);
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    public static Date getSomeMinute(Date date, int minute) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.MINUTE, minute);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    public static Date getSomeMonth(Date date, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.MONTH, month + getMonthValue(date) - 1);
        calendar.set(Calendar.DATE, 1);
        calendar.set(Calendar.HOUR_OF_DAY, getHourValue(date));
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    public static Date getLastDayOfMonth(Date date, int hour) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int month = calendar.get(Calendar.MONTH);
        calendar.set(Calendar.MONTH, month + 1);
        calendar.set(Calendar.DATE, 0);
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();

    }

    public static Date getSomeDay(Date date, int days) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DATE, days + getDayValue(date));
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    public static Date getLastMonthOfYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.MONTH, 11);
        return calendar.getTime();
    }

    public static Date getFirstMonthOfYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.MONTH, 0);

        return calendar.getTime();
    }

    public static Date getPreMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DATE, -1);
        return calendar.getTime();
    }

    public static List<String> getSecondForDay(Date date, int maxH) {
        if (maxH > 24) {
            return null;
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int size = maxH * 60 * 60;
        List<String> list = new ArrayList<String>();
        for (int i = 0; i < size; i++) {
            c.set(Calendar.HOUR_OF_DAY, 0);
            c.set(Calendar.MINUTE, 0);
            c.set(Calendar.SECOND, 0);
            c.set(Calendar.MILLISECOND, 0);
            c.add(Calendar.SECOND, i);
            list.add(DateToLongStr(c.getTime()));
            System.out.println(DateToLongStr(c.getTime()));
        }
        return list;
    }

    public static boolean isBase64(String str) {
        String base64Pattern = "^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$";
        return Pattern.matches(base64Pattern, str);
    }

    // 将这种格式的"Thu May 10 14:13:02 CST 2012"date 转为 yyyy-MM-dd HH:mm:ss 这种格式
    public static String DateStrTimeToLongStr(String dateStr) {
        DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss 'CST' yyyy", Locale.US);
        Date date = null;
        try {
            // 把字符串转换成CST日期类型
            date = df.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 把CST格式转换成普通日期格式
        return sdf.format(date);
    }

    //

    /**
     * 日期转换成字符串
     *
     * @param aDate        日期
     * @param dateSpan     时间分割0 为没分割
     * @param dateTimeSpan 日期和时间的分割字符 0 没分割符
     * @param timeSpan     时间的分割字符
     * @return Description of the Returned Value
     */
    public static String DateTimeToStr(Date aDate, char dateSpan, char dateTimeSpan, char timeSpan) {
        if (aDate == null) {
            return null;
        }
        StringBuffer dataBuf = new StringBuffer(20);
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(aDate);
        dataBuf.append(calendar.get(Calendar.YEAR));
        if (dateSpan != 0) {
            dataBuf.append(dateSpan);
        }
        int month = calendar.get(Calendar.MONTH) + 1;
        appendInt(dataBuf, month);
        if (dateSpan != 0) {
            dataBuf.append(dateSpan);
        }
        int date = calendar.get(Calendar.DATE);
        appendInt(dataBuf, date);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int min = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND);
        if (hour + min + second > 0) {
            if (dateTimeSpan != 0) {
                dataBuf.append(dateTimeSpan);
            }
            appendInt(dataBuf, hour);
            if (timeSpan != 0) {
                dataBuf.append(timeSpan);
            }
            appendInt(dataBuf, min);
            if (timeSpan != 0) {
                dataBuf.append(timeSpan);
            }
            appendInt(dataBuf, second);
        }
        return dataBuf.toString();
    }

    public static Date getDate(int year, int month, int day, int hour, int min, int sec) {
        Calendar cl = Calendar.getInstance();
        cl.set(year, month, day, hour, min, sec);
        return cl.getTime();
    }

    private static void appendInt(StringBuffer buf, int nDate) {
        if (nDate < 10) {
            buf.append("0");
        }
        buf.append(nDate);
    }

    /**
     * 计算两个日期间的分钟数，截止日期必须大于起始日期
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static long calcMinuteBetween2Dates(Date startDate, Date endDate) {
        if (startDate.compareTo(endDate) > 0) {
            return 0;
        }
        long start = startDate.getTime();
        long end = endDate.getTime();
        return (end - start) / (1000 * 60);
    }

    /**
     * 每15分钟
     *
     * @param date
     * @return
     */
    public static Date getCyc15Minute(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, getHourValue(date));
        int minute = getMinuteValue(date) / 15 * 15;
        calendar.set(Calendar.MINUTE, minute);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 每5分钟 ,当前时间设置为最靠近的上一个5分钟间隔的时刻
     *
     * @param date
     * @return
     */
    public static Date getCyc5Minute(Date date) {
        Date curr = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, getHourValue(curr));
        int minute = getMinuteValue(curr) / 5 * 5;
        calendar.set(Calendar.MINUTE, minute);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 获取指定日期去年的日期
     *
     * @return
     */
    public static String getDateOfLastYear(Date date) {
        // Date Format will be display
        SimpleDateFormat aSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        GregorianCalendar aGregorianCalendar = new GregorianCalendar();
        aGregorianCalendar.setTime(date);
        // Get last month GregorianCalendar object
        aGregorianCalendar.set(Calendar.YEAR, aGregorianCalendar.get(Calendar.YEAR) - 1);
        // Format the date to get year and month
        String currentYearAndMonth = aSimpleDateFormat.format(aGregorianCalendar.getTime());
        return currentYearAndMonth;
    }

    /**
     * 按照采集时间把当前时间转发为15分钟，30,45分钟格式
     *
     * @param startDate
     * @return
     */
    public static Date parseToCollectTime(Date startDate) {
        Calendar cr = Calendar.getInstance();
        cr.setTime(startDate);
        int minuts = cr.get(Calendar.MINUTE);
        if (minuts < 15) {
            minuts = 0;
        } else if (minuts < 30) {
            minuts = 15;
        } else if (minuts < 45) {
            minuts = 30;
        } else if (minuts >= 45) {
            minuts = 45;
        }
        cr.set(Calendar.MINUTE, minuts);
        cr.set(Calendar.SECOND, 0);
        cr.set(Calendar.MILLISECOND, 0);
        return cr.getTime();
    }

    public static String getWeekCN(int week) {
        if (week == 7) {
            return "周日";
        } else if (week == 1) {
            return "周一";
        } else if (week == 2) {
            return "周二";
        } else if (week == 3) {
            return "周三";
        } else if (week == 4) {
            return "周四";
        } else if (week == 5) {
            return "周五";
        } else if (week == 6) {
            return "周六";
        } else {
            return "非法字符";
        }

    }

    public static Date getFirstDayOfYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        return calendar.getTime();
    }

    public static byte[] Cp56time2atoByte(Calendar date) {
        short msec = (short) (date.get(Calendar.SECOND) * 1000 + date.get(Calendar.MILLISECOND));
        byte min = (byte) date.get(Calendar.MINUTE);
        byte hour = (byte) date.get(Calendar.HOUR_OF_DAY);
        ;
        byte day_m = (byte) date.get(Calendar.DAY_OF_MONTH);
        byte day_w = (byte) (date.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY ? 7
                : date.get(Calendar.DAY_OF_WEEK) - 1);
        byte month = (byte) (date.get(Calendar.MONTH) + 1);
        byte year = (byte) (date.get(Calendar.YEAR) - 2000);
        byte[] time = new byte[7];
        time[0] = (byte) (msec & 0xFF);
        time[1] = (byte) ((msec >> 8) & 0xFF);
        time[2] = (byte) (min & 0x3F);
        time[3] = (byte) (hour & 0x1F);
        time[4] = (byte) ((day_m & 0x1F) | (day_w << 5));
        time[5] = month;
        time[6] = year;
        return time;
    }

    public static Calendar Cp56time2atoDate(byte[] buf) {
        if (buf == null || buf.length < 7) {
            return null;
        }
        Calendar date = Calendar.getInstance();
        date.set(ProtocolUtils.Unsignd(buf[6]) + 2000, ProtocolUtils.Unsignd(buf[5]) - 1,
                ProtocolUtils.Unsignd((byte) (buf[4] & 0x1F)), ProtocolUtils.Unsignd((byte) (buf[3] & 0x1F)),
                ProtocolUtils.Unsignd((byte) (buf[2] & 0x3F)), ProtocolUtils.byteToShort(buf[1], buf[1]) / 1000);
        return date;
    }

    public static boolean isFirstDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        return day == 1 ? true : false;
    }

    /**
     * monthday(2位)+weekday(2位)
     *
     * @return
     */
    public static String getTodayWeekStr() {
        Calendar now = Calendar.getInstance();
        int monthday = now.get(Calendar.DAY_OF_MONTH);
        int weekday = now.get(Calendar.DAY_OF_WEEK);
        String result = StringUtil.AddjustLength(String.valueOf(monthday), 2, "0")
                + StringUtil.AddjustLength(String.valueOf(weekday), 2, "0");
        return result;
    }

    public static Date getToday() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 获取服务器启动时间
     */
    public static Date getServerStartDate() {
        long time = ManagementFactory.getRuntimeMXBean().getStartTime();
        return new Date(time);
    }

    /**
     * 计算两个时间差
     */
    public static String getDatePoor(Date endDate, Date nowDate) {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        return day + "天" + hour + "小时" + min + "分钟";
    }

    public static int since2000ToSeconds(Date date) {
        long ms = date.getTime();
        return (int) ((ms - FIRSTYEAR_2000) / 1000);
    }

}
